layui.use(['form','treeSelect','layer','upload','laydate','tree','table','laytpl','element','layedit'],function(){
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var newsDetail;
    //--------------------------------
    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/upload/editPicUpload' //接口url
                ,type: 'post' //默认post
            }
        });
        newsDetail=layedit.build('newsDetail',{
            height: 160 //设置编辑器高度
        }); //建立编辑器
    });

    // --------------------------------


    var treeSelect= layui.treeSelect;//树形下拉框
    treeSelect.render({
        elem: '#tree-select',
        data: '/newsType/selectTree',
        type: 'get',
        placeholder: '默认添加顶级菜单',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false

        click: function(data){
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            $("#tree-select").val("0");
        }
    });
    var upload = layui.upload;
    //上传头像

    upload.render({
        elem: '.userFaceBtn',
        field: 'uploadFile',//上传文件的名称，要和接口参数名称相同
        url: '/upload/pic',
        //multiple:true,
        done: function(res, index, upload){
            if (res.code === 0) {
                layer.msg(res.msg);
                $("#uploadImage").attr("src",res.url);
                $("#photo").val(res.url);
            } else {
                layer.msg(res.msg);
            }
        }
    });
    //检测项目数据
    $.get("/newsType/all",function (newsTypeId) {

        $.each(newsTypeId.data,function (i,ele) {
            $("#newsTypeId").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
        });
        form.render('select');
    });
    form.on("submit(addUser)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post("/news/add",{
             id : $(".id").val(),
             title : $(".title").val(),  //新闻标题
             newsTypeId : $("#tree-select").val(), //新闻类型
             source : $(".source").val(),  //新闻来源
             brief : $(".brief").val(),  //新闻类型
             pic : $(".pic").val(),
             isHot: $(".isHot").children("input:checked").val(),  //新闻热门
             newsDetail: layui.layedit.getContent(newsDetail),
             createBy: $(".createBy").val(),
             remarks: $(".remarks").val(),

         },function(res){
        //
         })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("新闻添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})